// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { companyBase } from "../../../../script/game/control/Item/ItemBase";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    company: companyBase = null;
    mode: number = 1; //招聘
    count: number = 0;
    maxCount: number = 0;
    start () {
        this.company = game.playerMgr.bag.companyBag.getItem(this._args.id);
        this.initData();
        this.addBtn();
    }
    addBtn() {
        this.touchEndEventScale(this.nodes.add, () => {
            this.count += 1;
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.reduce, () => {
            this.count -= 1;
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.ensure, () => {
            if(this.mode) {
                this.company.addWorker(this.count);
            } else {
                this.company.delWorker(this.count);
            }
            this.initData();
            ccs.mainEmitter.emit('upInfo');
            this._close();
        })
        this.touchEndEventScale(this.nodes.turnIn, () => {
            this.mode = 1;
            this.initData();
            this.nodes.turnOut.active = true;
            this.nodes.turnIn.active = false;
            this.nodes.mode.getComponent(cc.Label).string = "招聘";
        })
        this.touchEndEventScale(this.nodes.turnOut, () => {
            this.mode = 0;
            this.initData();
            this.nodes.turnOut.active = false;
            this.nodes.turnIn.active = true;
            this.nodes.mode.getComponent(cc.Label).string = "解雇";
        })
    }
    initData() {
        this.count = 0;
        this.nodes.slider.getComponent(cc.Slider).progress = 0;
        if(this.mode) {
            this.maxCount = this.company.company.maxWorker - this.company.company.workers;
        } else {
            this.maxCount = this.company.calUnworkers();
        }
        this.limitCount();
    }
    onSlider(tar) {
        let num = tar.progress;
        this.count = Math.floor(this.maxCount * num);
        this.limitCount();
    }
    limitCount() {
        this.nodes.money.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.count);
        this.nodes.maxMoney.getComponent(cc.Label).string = "最大：" + ccs.gf.getOmitNum(this.maxCount);
    }

}
